package com.navi.loaddata.core.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import javax.annotation.PostConstruct;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * JdbcUtil2
 *
 * @author Matthew
 * @date 2018-07-10
 */
@Component
public class JdbcUtil2 {
  public static ComboPooledDataSource GP_PROD_POOL;
  public static ComboPooledDataSource ORACLE_PROD_POOL;

  @Value("${spring.datasource.secondary.url}")
  private String secondaryUrl;
  @Value("${spring.datasource.secondary.username}")
  private String secondaryUser;
  @Value("${spring.datasource.secondary.password}")
  private String secondaryPassword;

  @Value("${spring.datasource.mes.url}")
  private String mesUrl;
  @Value("${spring.datasource.mes.username}")
  private String mesUsername;
  @Value("${spring.datasource.mes.password}")
  private String mesPassword;

  @PostConstruct
  public void init() throws PropertyVetoException {
//    setSecondaryDataSource();
    setMesDataSource();
  }

//  public void setSecondaryDataSource() throws PropertyVetoException {
//    ComboPooledDataSource tempPool = new ComboPooledDataSource();
//    tempPool.setInitialPoolSize(3);
//    tempPool.setMinPoolSize(0);
//    tempPool.setMaxPoolSize(0);
//
//    tempPool.setUser(secondaryUser);
//    tempPool.setPassword(secondaryPassword);
//    tempPool.setDriverClass("org.postgresql.Driver");
//    tempPool.setJdbcUrl(secondaryUrl);
//    GP_PROD_POOL = tempPool;
//  }
  public void setMesDataSource() throws PropertyVetoException {
    ComboPooledDataSource tempPool = new ComboPooledDataSource();
    tempPool.setInitialPoolSize(20);
    tempPool.setMinPoolSize(10);
    tempPool.setMaxPoolSize(50);

    tempPool.setUser(mesUsername);
    tempPool.setPassword(mesPassword);
    tempPool.setDriverClass("oracle.jdbc.driver.OracleDriver");
    tempPool.setJdbcUrl(mesUrl);
    ORACLE_PROD_POOL = tempPool;
  }

  public static QueryRunner getGpProdRunner() {
    QueryRunner queryRunner = new QueryRunner(GP_PROD_POOL);
    return queryRunner;
  }
  public static QueryRunner getMesProdRunner() {
    QueryRunner queryRunner = new QueryRunner(ORACLE_PROD_POOL);
    return queryRunner;
  }
}
